package com.hanbit.common;

import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Locale;

/**
 * Created by synn on 2014. 8. 13..
 */
@Controller
public class FileController {
    @RequestMapping(value = "/file_uploader", method = RequestMethod.POST)
    public String writeFile(Locale locale, Model model, HttpServletRequest request) {
        LoggerFactory.getLogger("Welcome imageUpload!! The client locale is "+locale);

        String filename = "";
        String filenameFront = "";
        String filenameExt = "";
        String convFilename = "";
        FileSystemResource uploadDir = new FileSystemResource("/Users/synn/Documents/workspace-sts/startup/src/main/webapp/upload/");

        MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
        MultipartFile file = multipart.getFile("Filedata");
        String callBack = multipart.getParameter("callback_func");
        if (!file.isEmpty()) {

            filename = file.getOriginalFilename();
            filenameFront = filename.substring(filename.lastIndexOf("."));
            filenameExt = filename.substring(filename.lastIndexOf("."), filename.length());
            //convFilename = Tool.getCurrentDayTimeMill() + filenameExt;
            convFilename = "";
            LoggerFactory.getLogger("OriginalFilename : " + filename);
            LoggerFactory.getLogger("OriginalFilename front : " + filenameFront);
            LoggerFactory.getLogger("OriginalFilename ext : " + filenameExt);
            LoggerFactory.getLogger("uploadDir : {}" + uploadDir.getPath());

            try {
                if (file.getSize() > 0) {
                    File out = new File(uploadDir.getPath() + "/" + filenameExt);
                    FileCopyUtils.copy(file.getBytes(), out);
                    convFilename = out.getName();
                    LoggerFactory.getLogger("ConvertFilename : " + convFilename);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        model.addAttribute("filename", convFilename);
        model.addAttribute("callback_func", callBack);
        return "util/callback";
    }

    @RequestMapping(value = "/file_uploader_html5", method = RequestMethod.POST)
    public String writeFile5() {

        return "util/file_uploader_html5";
    }

    @RequestMapping(value = "/file_uploader_html5", method = RequestMethod.GET)
    public String writeFile5get() {

        return "util/file_uploader_html5";
    }

}
